<template>
  <div class="system-menu-container">

    <div class="">

      <el-card shadow="hover" class="layout-padding-auto">
        <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
          <el-table-column prop="id" label="编号" show-overflow-tooltip></el-table-column>
          <el-table-column prop="template" label="随访名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="start_time_str" label="随访时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="call_exec_time" label="通话时长" show-overflow-tooltip></el-table-column>
          <el-table-column prop="content" label="状态" show-overflow-tooltip>
            <template #default="scope">
              <div v-if="scope.row.call_status == 'recv_bye'">接通</div>
              <div v-if="scope.row.call_status == 'send_bye'">接通</div>
              <div v-if="scope.row.call_status == 'recv_refuse'">未接通</div>
              <div v-if="scope.row.call_status == 'send_refuse'">未接通</div>
              <div v-if="scope.row.call_status == 'send_cancel'">未接通</div>
            </template>
          </el-table-column>
          <el-table-column label="标记" show-overflow-tooltip>
						<template #default="scope">
							{{ scope.row.is_patient_sign ? '已标记' : '未标记' }}
						</template>
					</el-table-column>

          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button size="small" text type="primary" @click="show( scope.$index)">查看</el-button>
              <el-button size="small" text type="primary" @click="signPatient(scope.row)">标记</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            @size-change="onHandleSizeChange"
            @current-change="onHandleCurrentChange"
            class="mt15"
            :pager-count="5"
            :page-sizes="[10, 20, 30]"
            v-model:current-page="state.tableData.param.pageNum"
            background
            v-model:page-size="state.tableData.param.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="state.tableData.total"
        >
        </el-pagination>
      </el-card>
    </div>

    <el-dialog
        v-if="state.form"
        v-model="state.dialogFormVisible"
        title="对话详情"
        width="900px"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="close"
    >
      <div class="dialog-header">
        <el-table :data="[state.form]" border header-cell-class-name="bg-color">
          <el-table-column prop="id" label="编号" show-overflow-tooltip></el-table-column>
          <el-table-column prop="template" label="随访名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="start_time_str" label="随访时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="call_exec_time" label="通话时长" show-overflow-tooltip></el-table-column>
          <el-table-column prop="content" label="状态" show-overflow-tooltip>
            <template #default="scope">
              <div v-if="scope.row.call_status == 'recv_bye'">接通</div>
              <div v-if="scope.row.call_status == 'send_bye'">接通</div>
              <div v-if="scope.row.call_status == 'recv_refuse'">未接通</div>
              <div v-if="scope.row.call_status == 'send_refuse'">未接通</div>
              <div v-if="scope.row.call_status == 'send_cancel'">未接通</div>
            </template>
          </el-table-column>

        </el-table>

      </div>
      <div class="message" style="margin-top:15px;">
        <ul style="height: 55vh;overflow-y: scroll">
          <li v-for="item in state.form.messages">

            <div class="main" :class="{ self: item.self==1 }">

              <div class="text" v-if="item.content">

                <el-icon color="#B4AFAF" size="15"
                         v-if="item.self==0"
                         style="cursor: pointer;color:#47a1fc"
                         @click="play(item.exec_start_time)"
                         icon="play-circle-fill">
                    <ele-VideoPlay/>
                </el-icon>



                <el-icon color="#B4AFAF" size="15"
                         v-if="item.self==1"
                   style="cursor: pointer;color:green"
                   @click="play(item.exec_end_time)"
                   icon="play-circle-fill"
                > <ele-VideoPlay/></el-icon>
                {{ item.content }}

                <i class="el-icon-video-play icon_style"></i>
              </div>
            </div>
          </li>
        </ul>
        <AudioPlayer ref="audioPlayerRef" v-if="state.form.upload_url" :fileurl="state.form.upload_url"></AudioPlayer>
      </div>
    </el-dialog>

    <SignPatientDialog ref="signPatientDialogRef" @refresh="getTableData()"></SignPatientDialog>
  </div>
</template>

<script setup name="systemDic">
import {ElMessageBox, ElMessage} from 'element-plus';
import {RecordLogApi} from "/@/api/record/log";
import {formatDuration, formatTimestamp} from "/@/utils/timeUtil";
import AudioPlayer from "/@/components/voice/AudioPlayer.vue";

import {useRouter} from 'vue-router';
const SignPatientDialog = defineAsyncComponent(() => import('./signPatient.vue'));
// 引入组件
const Breadcrumb = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/breadcrumb.vue'));
const router = useRouter();
const route = useRoute();
const audioPlayerRef = ref(null)
const signPatientDialogRef = ref(null);

// 定义变量内容
const state = reactive({
  dialogFormVisible: false,
  form: {},
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      name: "",
      status: ""
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  const param = {
    page_index: state.tableData.param.pageNum,
    page_size: state.tableData.param.pageSize,
    pcw_id: route.query.pcw_id
  }
  RecordLogApi().getPatientAiDetail(param).then(res => {
    state.tableData.total = res.data.total
    state.tableData.data = res.data.data
    state.tableData.loading = false;
    state.tableData.data.forEach((item) => {
      var raw = JSON.parse(item.raw)
      item.content = raw.map((item) => {
        return "问:" + item.question + "  答:" + item.answer
      }).join(";");
      item.start_time_str = formatTimestamp(item.start_time * 1000)
      item.call_exec_time = formatDuration(item.end_time - item.start_time)
    })
    console.log(state.tableData.data)
  })
};

const show = (index) => {
  state.form = state.tableData.data[index]
  var message = JSON.parse(state.form.raw)
  var new_message = []
  message.forEach((item)=>{
    new_message.push({
      content: item.question,
      self: 0,
      exec_start_time: item.exec_start_time,
      exec_end_time: item.exec_end_time,
      speech_start_time: item.speech_start_time
    })
    new_message.push({
      content: item.answer,
      self: 1,
      exec_start_time: item.exec_start_time,
      exec_end_time: item.exec_end_time,
      speech_start_time: item.speech_start_time
    })
  })
  state.form.messages = new_message
  console.log(state.form)
  state.dialogFormVisible = true

};

const signPatient = (row) => {
	signPatientDialogRef.value.openDialog({ rel_type: 1, rel_id: row.id, patient_id: route.query.id, });
};

const close = ()=>{
  console.log('close')
   state.form = {}
   state.form.upload_url = ''
   state.dialogFormVisible = false
}

const play= (start)=>{
  console.log(start)
  var startTime =  Math.floor(start)
  audioPlayerRef.value.turn(startTime);
}
// 分页改变
const onHandleSizeChange = (val) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
:deep() .bg-color{
  background:#47a1fc !important;
  color:white !important;
}
li {
  margin-bottom: 15px;
  list-style: none;
}

.self {
  text-align: right;
  color: black !important;
}
</style>
